package com.project.estate.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.estate.car.service.CarService;
import com.project.estate.commons.bean.Car;
import com.project.estate.commons.bean.ChargeItem;
import com.project.estate.commons.config.JsonResult;
import com.project.estate.commons.config.ResultTool;
import com.project.estate.commons.handler.*;
import com.project.estate.commons.mapper.CarMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Resource
    private CarMapper carMapper;

    @Override
    public JsonResult addCar(Car car) {
        boolean b = save(car);
        if (!b) {
            // 抛异常
            throw new CarAddException("添加失败");
        }
        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteCar(String picId) {
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("owner_id", picId);
        updateWrapper.set("delete_status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new CarDeleteException("删除失败");
        }
        return ResultTool.success("success");

    }

    //查询
    @Override
    public JsonResult findCar(String picId) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", picId);
        List<Car> list = list(queryWrapper);
        if (list.isEmpty()) {
            throw new CarFindException("未查询到");
        }
        return ResultTool.success(list);
    }

    //修改
    @Override
    public JsonResult updateCar(String picId, Car car) {
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("owner_id", picId);
        updateWrapper.set("car_number",car.getCarNumber());
        updateWrapper.set("color",car.getColor());
        updateWrapper.set("picture",car.getPicture());
        updateWrapper.set("remark",car.getRemark());
        boolean update = update(updateWrapper);
        if (!update) {
            throw new CarUpdateException("修改失败");
        }
        return ResultTool.success("success");
    }

    //查询
    @Override
    public JsonResult findAllCars() {
        List<Car> list = list();
        if (list.isEmpty()) {
            throw new CarFindException("未查询到数据");
        }
        return ResultTool.success(list);
    }
}
